<?php
/**
 * file:GW2EOS.php
 * This file is part of J-Game.
 *
 * @link    https://www.f4soft.com
 * @document https://www.f4soft.com
 * @contact 173120209@qq.com
 * @autho   :jackliang
 */
namespace F4soft\glass;

use F4soft\library\Database;

class GW2EOS {
    private static $instance;

    /**
     * @return \F4soft\glass\GW2EOS
     */
    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function beforeMerge() {
        $sql=file_get_contents(__DIR__ . "/../../sql/pepm_data.sql");
        Database::getInstanceSlave()->query($sql);
    }

    private function mergeComment() {
        $commmetLists = Database::getInstance()->select("comment", "*");
        Database::getInstanceSlave()->query("truncate table pepm_comment");
        foreach ($commmetLists as $k => $v) {
            echo "mergeComment" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_comment", $v);
        }
    }

    private function mergeConfig() {
        $commmetLists = Database::getInstance()->select("config", "*");
        Database::getInstanceSlave()->query("truncate table pepm_config");
        foreach ($commmetLists as $k => $v) {
            echo "mergeConfig" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_config", $v);
        }
    }

    private function mergeConfigHistoy() {
        $commmetLists = Database::getInstance()->select("config_history", "*");
        Database::getInstanceSlave()->query("truncate table pepm_config_history");
        foreach ($commmetLists as $k => $v) {
            echo "mergeConfigHistory" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_config_history", $v);
        }
    }

    private function mergeCustomer() {
        $commmetLists = Database::getInstance()->select("customer", "*");
        Database::getInstanceSlave()->query("truncate table pepm_customer");
        foreach ($commmetLists as $k => $v) {
            echo "mergeCustomer" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_customer", $v);
        }
    }

    private function mergeData() {
        $commmetLists = Database::getInstance()->select("data", "*");
        Database::getInstanceSlave()->query("truncate table pepm_data");
        foreach ($commmetLists as $k => $v) {
            echo "mergeData" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_data", $v);
        }
    }

    private function mergeCustomerWx() {
        $commmetLists = Database::getInstance()->select("customer_wx", "*");
        Database::getInstanceSlave()->query("truncate table pepm_customer_wx");
        foreach ($commmetLists as $k => $v) {
            echo "mergeCustomerWx" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_customer_wx", $v);
        }
    }

    private function mergeEntity() {
        $commmetLists = Database::getInstance()->select("entity", "*");
        Database::getInstanceSlave()->query("truncate table pepm_entity");
        foreach ($commmetLists as $k => $v) {
            echo "mergeEntity" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_entity", $v);
        }
    }

    private function mergeFile() {
        $commmetLists = Database::getInstance()->select("file", "*");
        Database::getInstanceSlave()->query("truncate table pepm_file");
        foreach ($commmetLists as $k => $v) {
            echo "mergeFile" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_file", $v);
        }
    }

    private function mergeHistory() {
        $commmetLists = Database::getInstance()->select("history", "*");
        Database::getInstanceSlave()->query("truncate table pepm_history");
        foreach ($commmetLists as $k => $v) {
            echo "mergeHistory" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_history", $v);
        }
    }

    private function mergeMsg() {
        $commmetLists = Database::getInstance()->select("msg", "*");
        Database::getInstanceSlave()->query("truncate table pepm_msg");
        foreach ($commmetLists as $k => $v) {
            echo "mergeMsg" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_msg", $v);
        }
    }

    private function mergeOpen() {
        $commmetLists = Database::getInstance()->select("open", "*");
        Database::getInstanceSlave()->query("truncate table pepm_open");
        foreach ($commmetLists as $k => $v) {
            echo "mergeOpen" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_open", $v);
        }
    }

    private function mergeUsr() {
        $commmetLists = Database::getInstance()->select("usr", "*");
        Database::getInstanceSlave()->query("truncate table pepm_usr");
        foreach ($commmetLists as $k => $v) {
            echo "mergeUsr" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_usr", $v);
        }
    }

    private function mergeSpaceUsr() {
        $commmetLists = Database::getInstance()->select("space_usr", "*");
        Database::getInstanceSlave()->query("truncate table pepm_space_usr");
        foreach ($commmetLists as $k => $v) {
            echo "mergeSpaceUsr" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_space_usr", $v);
        }
    }

    private function mergeTrace() {
        $commmetLists = Database::getInstance()->select("trace", "*");
        Database::getInstanceSlave()->query("truncate table pepm_trace");
        foreach ($commmetLists as $k => $v) {
            echo "mergeTrace" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_trace", $v);
        }
    }

    private function mergePunch() {
        $commmetLists = Database::getInstance()->select("punch", "*");
        Database::getInstanceSlave()->query("truncate table pepm_punch");
        foreach ($commmetLists as $k => $v) {
            echo "mergePunch" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_punch", $v);
        }
    }

    private function mergeQueue() {
        $commmetLists = Database::getInstance()->select("queue", "*");
        Database::getInstanceSlave()->query("truncate table pepm_queue");
        foreach ($commmetLists as $k => $v) {
            echo "mergeQueue" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_queue", $v);
        }
    }

    private function mergeSpace() {
        $commmetLists = Database::getInstance()->select("space", "*");
        Database::getInstanceSlave()->query("truncate table pepm_space");
        foreach ($commmetLists as $k => $v) {
            echo "mergeSpace" . $v['id'] . PHP_EOL;
            unset($v['id']);
            if (isset($v['type'])) {
                $v['catagory'] = $v['type'];
                unset($v['type']);
            }
            Database::getInstanceSlave()->insert("pepm_space", $v);
        }
    }

    public function main() {
        ini_set("memory_limit", "1024M");
        $this->beforeMerge();
        $this->mergeComment();
        $this->mergeConfig();
        $this->mergeConfigHistoy();
        $this->mergeCustomer();
        $this->mergeCustomerWx();
        $this->mergeData();
        $this->mergeEntity();
        $this->mergeFile();
        $this->mergeHistory();
        $this->mergeMsg();
        $this->mergeOpen();
        $this->mergePunch();
        $this->mergeQueue();
        $this->mergeSpace();
        $this->mergeSpaceUsr();
        $this->mergeTrace();
        $this->mergeUsr();
    }
}